#include <iostream>  
  
using namespace std;  
  
struct ListNode {  
    int val;  
    ListNode *next;  
    ListNode(int x) : val(x), next(NULL) {}  
};  
  
ListNode* deleteDuplicates(ListNode* head) {  
    ListNode* cur = head;  
    ListNode* pre = NULL;  
    while (cur != NULL && cur->next != NULL) {  
        if (cur->val == cur->next->val) {  
            while (cur->next != NULL && cur->val == cur->next->val) {  
                cur = cur->next;  
            }  
            if (pre == NULL) {  
                head = cur->next;  
            } else {  
                pre->next = cur->next;  
            }  
        } else {  
            pre = cur;  
        }  
        cur = cur->next;  
    }  
    return head;  
}  
  
void printList(ListNode* head) {  
    ListNode* cur = head;  
    while (cur != NULL) {  
        cout << cur->val << " ";  
        cur = cur->next;  
    }  
    cout << endl;  
}  
  
int main() {  
    ListNode* head = new ListNode(1);  
    head->next = new ListNode(1);  
    head->next->next = new ListNode(2);  
    head->next->next->next = new ListNode(3);  
    head->next->next->next->next = new ListNode(3);  
    head->next->next->next->next->next = new ListNode(4);  
    head->next->next->next->next->next->next = new ListNode(4);  
    head->next->next->next->next->next->next->next = new ListNode(5);  
    head->next->next->next->next->next->next->next->next = new ListNode(5);  
    head->next->next->next->next->next->next->next->next->next = new ListNode(5);  
    printList(head);  
    head = deleteDuplicates(head);  
    printList(head);  
    return 0;  
}